import { sendCommandMini } from '@/api/gps/command'
import service from '@/utils/request'
function sendCmd({ imei, type, attributes }: { imei: string; type: string; attributes: any }, config = {}) {
  const data = {
    imei: imei,
    type,
    // id: this.cmdId,
    sync: true,
    cacheSec: 25,
    password: null,
    attributes
  }
  return sendCommandMini(data, config)
}
export function intercomRequest(imei: string, channel: number) {
  const attributes = {
    // channel: 13,
    channel: channel,
    mediaType: 2, // 2对讲
    streamType: 0
  }
  return sendCmd({ imei, type: 'TYPE_SERVER_808_1078_AV_REQUEST', attributes })
}
export function intercomControl(imei: string, channel: number) {
  const attributes = {
    // channel: 13,
    channel: channel,
    cmd: 0,
    closeType: 0,
    switchType: 0
  }
  return sendCmd({ imei, type: 'TYPE_SERVER_808_1078_AV_CONTROL', attributes })
}
export function avRequest({ imei, channel, streamType, mediaType }: { imei: string; channel: number; streamType: number; mediaType: number }, config = {}) {
  //channel 13 2 0
  // <param type="ip" value="">服务器IP地址</param>
  // <param type="tcpPort" value="1078">实时视频服务器TCP端口</param>
  // <param type="udpPort" value="0">实时视频服务器UDP端口</param>
  // <param type="channel" value="1">摄像头编号</param>
  // <param type="mediaType" value="0">数据类型：0.音视频 1.视频 2.双向对讲 3.监听 4.中心广播 5.透传</param>
  // <param type="streamType" value="1">码流类型：0.主码流 1.子码流</param>
  const attributes = {
    ip: null,
    tcpPort: null,
    udpPort: null, //以上3个在服务器里填
    channel,
    mediaType, // 2对讲
    streamType
  }
  //   const params = ['', '', '', '' + channel, '2', '0']
  return sendCmd({ imei, type: 'TYPE_SERVER_808_1078_AV_REQUEST', attributes }, config)
}
export function avControl(imei: string, channel: number, cmd: number = 0) {
  //channel 13
  // <param type="channel" value="1">摄像头编号</param>
  // <param type="cmd" value="0">命令数字 0.关闭音视频 1.切换码流 2.暂停 3.恢复 4.关闭双向对讲</param>
  // <param type="closeType" value="0">关闭类型 0.关闭音视频 1.只关闭音频 2.只关闭视频</param>
  // <param type="switchType" value="0">切换码流类型：0.主码流 1.子码流</param>
  const attributes = {
    channel,
    cmd,
    closeType: 0,
    switchType: 0
  }
  //   const params = ['' + channel, '0', '0', '0']
  return sendCmd({ imei, type: 'TYPE_SERVER_808_1078_AV_CONTROL', attributes })
}

export function queryHistory(
  imei: string,
  channel: number,
  mediaType: number,
  streamType: number,
  storageType: number,
  startTime: string, //"yyyy-MM-dd HH:mm:ss"
  endTime: string
) {
  // <param type="channel" value="1">摄像头编号0表示所有通道</param>
  // <param type="startTime" value="">开始时间(YYMMDDHHMMSS,全0表示无起始时间)</param>
  // <param type="endTime" value="">结束时间(YYMMDDHHMMSS,全0表示无终止时间)</param>
  // <param type="warnBit1" value="">报警标志0~31(参考808协议文档报警标志位定义)</param>
  // <param type="warnBit2" value="">报警标志32~63</param>
  // <param type="mediaType" value="0">音视频资源类型：0.音视频 1.音频 2.视频 3.视频或音视频</param>
  // <param type="streamType" value="1">码流类型：0.所有码流 1.主码流 2.子码流</param>
  // <param type="storageType" value="1">存储器类型：0.所有存储器 1.主存储器 2.灾备存储器</param>
  const attributes = {
    channel,
    startTime,
    endTime,
    warnBit1: 0,
    warnBit2: 0,
    mediaType: 0,
    streamType: 1,
    storageType: 1
  }
  return sendCmd({ imei, type: 'TYPE_SERVER_808_1078_QUERY_HISTORY', attributes })
}
interface QueryHistory {
  imei: string
  channel: number | undefined
  mediaType: number
  streamType: number
  storageType: number
  startTime: string
  endTime: string
}
export function queryHistory2(data: QueryHistory) {
  const { imei, ...rest } = data
  return sendCmd({
    imei,
    type: 'TYPE_SERVER_808_1078_QUERY_HISTORY',
    attributes: {
      warnBit1: 0,
      warnBit2: 0,
      ...rest
    }
  })
}
export function historyAvRequest0(
  imei: string,
  channel: number,
  startTime: string, //"yyyy-MM-dd HH:mm:ss"
  endTime: string
) {
  // <param type="ip" value="">服务器IP地址</param>
  // <param type="tcpPort" value="1079">历史视频服务器TCP端口</param>
  // <param type="udpPort" value="0">历史视频服务器UDP端口</param>
  // <param type="channel" value="1">摄像头编号</param>
  // <param type="mediaType" value="0">音视频资源类型：0.音视频 1.音频 2.视频 3.视频或音视频</param>
  // <param type="streamType" value="1">码流类型：0.所有码流 1.主码流 2.子码流(如果此通道只传输音频,此字段置0)</param>
  // <param type="storageType" value="1">存储器类型：0.所有存储器 1.主存储器 2.灾备存储器</param>
  // <param type="playbackMode" value="0">回放方式：0.正常回放 1.快进回放 2.关键帧快退回放 3.关键帧播放 4.单帧上传</param>
  // <param type="playbackSpeed" value="1">快进或快退倍数：0.无效 1.1倍 2.2倍 3.4倍 4.8倍 5.16倍 (回放控制为1和2时,此字段内容有效,否则置0)</param>
  // <param type="startTime" value="">开始时间(YYMMDDHHMMSS,回放方式为4时,该字段表示单帧上传时间)</param>
  // <param type="endTime" value="">结束时间(YYMMDDHHMMSS,回放方式为4时,该字段无效,为0表示一直回放)</param>
  const attributes = {
    ip: null,
    tcpPort: null,
    udpPort: null, //以上3个在服务器里填
    channel,
    mediaType: 0,
    streamType: 2,
    storageType: 1,
    playbackMode: 0,
    playbackSpeed: 1,
    startTime,
    endTime
  }
  return sendCmd({ imei, type: 'TYPE_SERVER_808_1078_HISTORY_AV_REQUEST', attributes })
}
export interface HistoryAv {
  imei: string
  channel: number
  startTime: string
  endTime: string
  mediaType: number
  streamType: number
  storageType: number
}
export function historyAvRequest(data: HistoryAv) {
  const { imei, ...rest } = data
  return sendCmd({
    imei,
    type: 'TYPE_SERVER_808_1078_HISTORY_AV_REQUEST',
    attributes: {
      ip: null,
      tcpPort: null,
      // ip: '183.222.51.116',
      // tcpPort: 11089,
      udpPort: null, //以上3个在服务器里填
      playbackMode: 0,
      playbackSpeed: 1,
      ...rest
    }
  })
}

export interface HistoryAvCtl {
  imei: string
  channel: number
  playbackMode: 0 | 1 | 2 | 3 | 4 | 5 | 6
  playbackSpeed: number
  playbackTime: string
}
export function historyAvControl(data: HistoryAvCtl) {
  // <param type="channel" value="1">摄像头编号</param>
  // <param type="playbackMode" value="0">回放控制：0.开始回放 1.暂停回放 2.结束回放 3.快进回放 4.关键帧快退回放 5.拖动回放 6.关键帧播放</param>
  // <param type="playbackSpeed" value="1">快进或快退倍数：0.无效 1.1倍 2.2倍 3.4倍 4.8倍 5.16倍 (回放控制为3和4时,此字段内容有效,否则置0)</param>
  // <param type="playbackTime" value="">拖动回放位置(YYMMDDHHMMSS,回放控制为5时,此字段有效)</param>
  const { imei, ...rest } = data
  return sendCmd({ imei, type: 'TYPE_SERVER_808_1078_HISTORY_AV_CONTROL', attributes: rest })
}

//condition任务执行条件  用 Ыt 位表示:
// bit0:WIFI,为1时表示 WIFI下可下载:
// bit1:LAN,为1时表示 LAN 连接时可下载:
// bit2:3G/4G,为1时表示 3G/4G 连接时可下载
export interface UploadAv {
  imei: string
  channel: number
  startTime: string
  endTime: string
  mediaType: number
  streamType: number
  storageType: number
  condition: number
}
export function uploadRequest(data: UploadAv) {
  const { imei, ...rest } = data
  return sendCmd({
    imei,
    type: 'TYPE_SERVER_808_1078_UPLOAD_REQUEST',
    attributes: {
      warnBit1: 0,
      warnBit2: 0,
      ...rest
    }
  })
}
//cmd 0:暂停;1:继续;2:取消
export function uploadControl(imei: string, serialNo: number, cmd: number) {
  return sendCmd({
    imei,
    type: 'TYPE_SERVER_808_1078_UPLOAD_CONTROL',
    attributes: {
      serialNo,
      cmd
    }
  })
}

export function useQueryDownloadTasksApi(imei) {
  return service.get('gps/upload/list', {
    params: {
      imei
    }
  })
}
